package com.aspire.core;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import jodd.cache.Cache;
import jodd.cache.LRUCache;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import net.sf.json.JSONArray;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.impl.calendar.CronCalendar;

import com.aspire.core.match.ua.object.Device;
import com.aspirecn.core.commons.IPUtils;
import com.aspirecn.core.object.IP;
import com.csvreader.CsvReader;

/**
 * IP地址文件整理
 */
public class ZhengliIPFile 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public ZhengliIPFile( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( ZhengliIPFile.class );
    }

    /**
     * Rigourous Test :-)
     * @throws ParseException 
     * @throws IOException 
     * @throws InterruptedException 
     */
    public void testApp() throws IOException
    {
    	String f = "D:\\PrivateFiles\\Desktop\\IP地址比对\\cmwap-cmnet.log";
    	File ipFile = new File(f);
    	
    	String sqlf = "D:\\PrivateFiles\\Desktop\\IP地址比对\\cmwap-cmnet.sql";
    	File ipsqlFile = new File(sqlf);
    	
    	List<String> ipList = FileUtils.readLines(ipFile);
    	
    	System.out.println( "start" );
    	for( String ip:ipList )
    	{
    		String tempIP = StringUtils.deleteWhitespace(ip);
    		//System.out.println( tempIP );
    		
    		if( StringUtils.isNotBlank(tempIP) )
    		{
    			System.out.println( tempIP );
    			
    			
    			if( StringUtils.contains(tempIP, "-") )
    			{
    				long[] iprange = IPUtils.getIPRange(tempIP);
    				System.out.println( tempIP );
    				for( long start = iprange[0]; start <= iprange[1]; start++ )
    				{
    					//System.out.println( "aa" + IPUtils.longToIP(start) );
    					FileUtils.writeStringToFile(ipsqlFile, tosql(IPUtils.longToIP(start)) + "\n", true);
    				}
    			}
    			else
    			{
    				//tosql( tempIP );
    				FileUtils.writeStringToFile(ipsqlFile, tosql(tempIP) + "\n", true);//写sql
    				
//    				IP obj = IPUtils.getIPObject(tempIP);
//    				if( obj.getGateway() != IP.CMWAP_GATEWAY )
//    				{
//    					FileUtils.writeStringToFile(ipsqlFile, tempIP + "\n", true);//写sql
//    				}
    			}
    		}
    	}
    }
    
    
    private String tosql( String ip )
    {
    	String deleteSql = "delete from gateway_master where type='cmwap' and `ip`='" + ip + "';";
    	String deleteSql2 = "delete from gateway_master where type='cmnet' and `ip`='" + ip + "';";
    	String insertSql = "insert into gateway_master(`ip`,`type`,`desc`,`udate`) values('" + ip + "','cmnet','excel',now());";
    	//System.out.println( deleteSql );
    	//System.out.println( insertSql );
    	
    	System.out.println( ip );
    	
    	if( !StringUtils.contains(ip, "/") && !StringUtils.equals(ip, "211.140.5115")
    			&& !StringUtils.equals(ip, "211.140.5103")
    			&& !StringUtils.equals(ip, "211.140.5115"))
    	{
    		IPUtils.ipToLong(ip);
    	}
    	
    	
    	return deleteSql + "\n" + deleteSql2 + "\n" + insertSql;
    }
}
